package com.lenovo.smarttraffic.zm;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author zm
 * @version 2.0
 * @date 2020/9/22 9:31
 */
public class RoadActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_road1, tv_road2, tv_road3, tv_road4, tv_road5, tv_road6, tv_road7;
    private BarChart bar_1;
    public static List<BarEntry> road1, road2, road3, road4, road5, road6, road7;
    private BarDataSet dataSet1, dataSet2, dataSet3, dataSet4, dataSet5, dataSet6, dataSet7;
    private Boolean b_road1 = true, b_road2 = true, b_road3 = true, b_road4 = true, b_road5 = true, b_road6 = true, b_road7 = true;

    private Boolean shuai = true;
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(runnable, 5000);
            initBar();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        handler.postDelayed(runnable, 5000);
    }

    @Override
    protected int getLayout() {
        return R.layout.road_ana_layout;
    }

    private void initView() {
        initToolBar("路况分析");
        tv_road1 = (TextView) findViewById(R.id.road1);
        tv_road1.setOnClickListener(this);
        tv_road2 = (TextView) findViewById(R.id.road2);
        tv_road2.setOnClickListener(this);
        tv_road3 = (TextView) findViewById(R.id.road3);
        tv_road3.setOnClickListener(this);
        tv_road4 = (TextView) findViewById(R.id.road4);
        tv_road4.setOnClickListener(this);
        tv_road5 = (TextView) findViewById(R.id.road5);
        tv_road5.setOnClickListener(this);
        tv_road6 = (TextView) findViewById(R.id.road6);
        tv_road6.setOnClickListener(this);
        tv_road7 = (TextView) findViewById(R.id.road7);
        tv_road7.setOnClickListener(this);
        bar_1 = (BarChart) findViewById(R.id.bar_1);
        road1 = new ArrayList<>();
        road2 = new ArrayList<>();
        road3 = new ArrayList<>();
        road4 = new ArrayList<>();
        road5 = new ArrayList<>();
        road6 = new ArrayList<>();
        road7 = new ArrayList<>();
        initBar();
    }

    private void initBar() {
        bar_1.setScaleEnabled(false);
        bar_1.setEnabled(false);
        bar_1.setDescription(null);
        bar_1.getLegend().setEnabled(false);
        initRoad();
        dataSet1 = new BarDataSet(road1, "学院路");
        dataSet2 = new BarDataSet(road2, "学院路");
        dataSet3 = new BarDataSet(road3, "学院路");
        dataSet4 = new BarDataSet(road4, "学院路");
        dataSet5 = new BarDataSet(road5, "学院路");
        dataSet6 = new BarDataSet(road6, "学院路");
        dataSet7 = new BarDataSet(road7, "学院路");

        dataSet1.setDrawValues(false);
        dataSet1.setColor(Color.parseColor("#CC1616"));
        dataSet1.setHighlightEnabled(false);

        dataSet2.setDrawValues(false);
        dataSet2.setColor(Color.parseColor("#DA87A3"));
        dataSet2.setHighlightEnabled(false);

        dataSet3.setDrawValues(false);
        dataSet3.setColor(Color.parseColor("#15A023"));
        dataSet3.setHighlightEnabled(false);

        dataSet4.setDrawValues(false);
        dataSet4.setColor(Color.parseColor("#3BEBDA"));
        dataSet4.setHighlightEnabled(false);

        dataSet5.setDrawValues(false);
        dataSet5.setColor(Color.parseColor("#B1421F"));
        dataSet5.setHighlightEnabled(false);

        dataSet6.setDrawValues(false);
        dataSet6.setColor(Color.parseColor("#162FBE"));
        dataSet6.setHighlightEnabled(false);

        dataSet7.setDrawValues(false);
        dataSet7.setColor(Color.parseColor("#967513"));
        dataSet7.setHighlightEnabled(false);

        List<IBarDataSet> dataList = new ArrayList<>();
        dataList.add(dataSet1);
        dataList.add(dataSet2);
        dataList.add(dataSet3);
        dataList.add(dataSet4);
        dataList.add(dataSet5);
        dataList.add(dataSet6);
        dataList.add(dataSet7);
        BarData barData = new BarData(dataList);
        barData.setBarWidth(0.1f);


        XAxis xAxis = bar_1.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawAxisLine(true);
        xAxis.setDrawGridLines(false);
        List<String> XList = new ArrayList<>();
        XList.add("周一");
        XList.add("周二");
        XList.add("周三");
        XList.add("周四");
        XList.add("周五");
        XList.add("周六");
        XList.add("周日");
        xAxis.setValueFormatter(new MyXdataFormatter(XList));


        YAxis axisRight = bar_1.getAxisRight();
        axisRight.setDrawAxisLine(true);
        axisRight.setDrawGridLines(true);
        axisRight.setAxisMaximum(5);
        axisRight.setAxisMinimum(0);
        axisRight.setLabelCount(6, true);

        YAxis axisLeft = bar_1.getAxisLeft();
        axisLeft.setDrawGridLines(false);
        axisLeft.setDrawAxisLine(true);

        List<String> YList = new ArrayList<>();
        YList.add("畅通");
        YList.add("缓行");
        YList.add("一般拥堵");
        YList.add("中度拥堵");
        YList.add("严重拥堵");
        axisLeft.setValueFormatter(new MyYdataFormatter(YList));
        bar_1.setData(barData);
        bar_1.groupBars(-0.5f, 0.3f, 0);
        bar_1.invalidate();
    }

    private void initRoad() {

        if (shuai) {
            for (int i = 0; i < 7; i++) {
                road1.add(new BarEntry(i, new Random().nextInt(5) + 1));
                road2.add(new BarEntry(i, new Random().nextInt(5) + 1));
                road3.add(new BarEntry(i, new Random().nextInt(5) + 1));
                road4.add(new BarEntry(i, new Random().nextInt(5) + 1));
                road5.add(new BarEntry(i, new Random().nextInt(5) + 1));
                road6.add(new BarEntry(i, new Random().nextInt(5) + 1));
                road7.add(new BarEntry(i, new Random().nextInt(5) + 1));
            }
            shuai = false;
        } else {
            road1.set(0, new BarEntry(0, new Random().nextInt(5) + 1));
            road2.set(0, new BarEntry(0, new Random().nextInt(5) + 1));
            road3.set(0, new BarEntry(0, new Random().nextInt(5) + 1));
            road4.set(0, new BarEntry(0, new Random().nextInt(5) + 1));
            road5.set(0, new BarEntry(0, new Random().nextInt(5) + 1));
            road6.set(0, new BarEntry(0, new Random().nextInt(5) + 1));
            road7.set(0, new BarEntry(0, new Random().nextInt(5) + 1));
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.road1:
                if (b_road1) {
                    dataSet1.setColor(Color.parseColor("#CC1616"), 0);
                    b_road1 = false;
                } else {
                    dataSet1.setColor(Color.parseColor("#CC1616"));
                    b_road1 = true;
                }

                break;
            case R.id.road2:
                if (b_road2) {
                    dataSet2.setColor(Color.parseColor("#DA87A3"), 0);
                    b_road2 = false;
                } else {
                    dataSet2.setColor(Color.parseColor("#DA87A3"));
                    b_road2 = true;
                }
                break;
            case R.id.road3:
                if (b_road3) {
                    dataSet3.setColor(Color.parseColor("#15A023"), 0);
                    b_road3 = false;
                } else {
                    dataSet3.setColor(Color.parseColor("#15A023"));
                    b_road3 = true;
                }
                break;
            case R.id.road4:
                if (b_road4) {
                    dataSet4.setColor(Color.parseColor("#3BEBDA"), 0);
                    b_road4 = false;
                } else {
                    dataSet4.setColor(Color.parseColor("#3BEBDA"));
                    b_road4 = true;
                }
                break;
            case R.id.road5:
                if (b_road5) {
                    dataSet5.setColor(Color.parseColor("#3BEBDA"), 0);
                    b_road5 = false;
                } else {
                    dataSet5.setColor(Color.parseColor("#3BEBDA"));
                    b_road5 = true;
                }
                break;
            case R.id.road6:
                if (b_road6) {
                    dataSet6.setColor(Color.parseColor("#B1421F"), 0);
                    b_road6 = false;
                } else {
                    dataSet6.setColor(Color.parseColor("#B1421F"));
                    b_road6 = true;
                }
                break;
            case R.id.road7:
                if (b_road7) {
                    dataSet7.setColor(Color.parseColor("#967513"), 0);
                    b_road7 = false;
                } else {
                    dataSet7.setColor(Color.parseColor("#967513"));
                    b_road7 = true;
                }
                break;
        }
        bar_1.invalidate();

    }


    public class MyXdataFormatter extends ValueFormatter {
        List<String> MyList = new ArrayList<>();

        public MyXdataFormatter(List<String> MyList) {
            this.MyList = MyList;
        }

        @Override
        public String getFormattedValue(float value) {
            return MyList.get((int) value);
        }
    }


    public class MyYdataFormatter extends ValueFormatter {
        List<String> MyYList = new ArrayList<>();

        public MyYdataFormatter(List<String> MyList) {
            this.MyYList = MyList;
        }

        @Override
        public String getFormattedValue(float value) {
            return MyYList.get((int) value);
        }
    }
}
